/*
 * @Description: 检测任务管理操作
 * @Author: zzt
 * @Date: 2022-04-28 13:47:59
 * @LastEditTime: 2022-10-22 10:58:24
 */

import { useMemo, useState } from 'react';
import { Button, Divider, Space } from 'antd';
import cs from 'classnames';
import { useStandardizedDetectionContext } from '../contexts/StandardizedDetectionContext';
import InspectionTasks from '@/components/ArchivesDetection';
import { findStandardizedDetectionById } from '@/api/standardizedDetection/detectionTask';
import DetectExecuting from '@/components/ArchivesDetection/conponentes/DetectExecuting';

const PageOperate = (props: any) => {
  const { classname } = props;
  const {
    selectItems,
    deleteItem,
    selectAT,
    selectFondId,
    addStandardizedList,
    editStandardizedList,
    performDetection,
    detectExecuting
  } = useStandardizedDetectionContext();

  const [taskValue, setTaskValue] = useState(selectAT);
  // 规范性检测任务弹窗
  const [createInspectionTasksVisible, setCreateInspectionTasksVisible] = useState(false);
  // 执行检测弹窗
  const [detectExecutingVisible, setDetectExecutingVisible] = useState(false);

  const deleteSelectStandardizedList = () => {
    deleteItem(selectItems, `您已选择了${selectItems.length}个检测任务，是否确认移除?`);
  };

  /* 执行检测 */
  const onPerform = () => {
    performDetection(selectItems[0]);
    setDetectExecutingVisible(detectExecuting);
  };

  /* 点击修改时，将选中的数据查询并赋值 */
  const onChange = () => {
    findStandardizedDetectionById(selectItems[0]).then((res: any) => {
      const { data } = res;
      setTaskValue(data);
      setCreateInspectionTasksVisible(true);
    });
  };

  /* 点击创建时触发 */
  const onCreate = () => {
    setTaskValue(selectAT.id);
    setCreateInspectionTasksVisible(true);
  };

  return (
    <div className={cs('filter-container', 'page-operate', classname)}>
      <p>规范性检测任务</p>
      <Space size="small">
        <Button
          disabled={selectItems.length !== 1}
          onClick={() => {
            onChange();
          }}
        >
          修改
        </Button>

        {selectItems.length > 0 ? (
          <Button
            onClick={() => {
              deleteSelectStandardizedList();
            }}
          >
            删除
          </Button>
        ) : (
          <Button disabled>删除</Button>
        )}
        {selectItems.length === 1 ? (
          <Button onClick={onPerform}>执行检测</Button>
        ) : (
          <Button disabled>执行检测</Button>
        )}
        <Button
          onClick={() => {
            onCreate();
          }}
        >
          新建检测任务
        </Button>
        <Divider type="vertical" />
        <Button>筛选</Button>
      </Space>
      {useMemo(
        () => (
          <DetectExecuting
            visible={detectExecutingVisible}
            onClose={() => {
              setDetectExecutingVisible(false);
            }}
          />
        ),
        [detectExecutingVisible]
      )}

      {/* 检测任务弹窗 */}
      {useMemo(
        () => (
          <InspectionTasks
            visible={createInspectionTasksVisible}
            onClose={() => {
              setCreateInspectionTasksVisible(false);
            }}
            archiveType={selectFondId}
            data={taskValue}
            addStandardizedList={addStandardizedList}
            editStandardizedList={editStandardizedList}
          />
        ),
        [createInspectionTasksVisible]
      )}
    </div>
  );
};

export default PageOperate;
